package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.AppUpdateTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;
import com.scpii.universal.util.SpringBeanFactory;

public class AppUpdateTabService implements BaseInterface<AppUpdateTab> {
	private static final Logger log = LoggerFactory
			.getLogger(AppUpdateTabService.class);

//	private BaseDao<AppUpdateTab> appUpdateTabDao = SpringBeanFactory.appUpdateTabDao;
	private BaseDao<AppUpdateTab> appUpdateTabDao;

	public AppUpdateTab get(Integer id) throws Exception {
		return appUpdateTabDao.get(id);
	}

	public List<AppUpdateTab> getAll() throws Exception {
		return appUpdateTabDao.getAll();
	}

	public AppUpdateTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<AppUpdateTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(AppUpdateTab obj) throws Exception {

	}

	public void update(AppUpdateTab obj) throws Exception {

	}

	public void delete(AppUpdateTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<AppUpdateTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return appUpdateTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		appUpdateTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return appUpdateTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<AppUpdateTab> sqlGetList(String sqls,
			Map<String, Object> argsMap) throws Exception {
		return appUpdateTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		appUpdateTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return appUpdateTabDao.sqlGetNum(sqls, argsMap);
	}

	//Setter
	public void setAppUpdateTabDao(BaseDao<AppUpdateTab> appUpdateTabDao) {
		this.appUpdateTabDao = appUpdateTabDao;
	}
	
	/**
	 * 获取需要更新的应用
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-10-18上午09:38:05
	 */
	public AppUpdateTab getUpdateApp(int appId,int osType)throws Exception{
		List<AppUpdateTab> appUpdateList = new ArrayList<AppUpdateTab>();
		
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("osType", osType);
		
		appUpdateList = this.hqlGetList("GET_APP_UPDATE_LIST", argsMap);
		
		if (appUpdateList!=null && appUpdateList.size()==1) {
			AppUpdateTab appUpdateTab = appUpdateList.get(0);
			appUpdateTab.setIntState(Const.APP_MUST_UPDATE);
			return appUpdateTab;
		}
		return null;
	}
	
	/**
	 * 获取需要更新的应用
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-10-18上午09:38:05
	 */
	public AppUpdateTab getUpdateApp(int appId,int osType,double dobAppUpdateVersion,AppUpdateTab maxVersionApp)throws Exception{
		//获取当前版本和最大版本之间需要更新的记录
		//验证是否必须更新
		if (checkIsUpdate(dobAppUpdateVersion, appId, osType,Const.APP_MUST_UPDATE)) {
			maxVersionApp.setIntState(Const.APP_MUST_UPDATE);
		}else {
			//验证是否有可选更新
			if (checkIsUpdate(dobAppUpdateVersion, appId, osType, Const.APP_CAN_UPDATE)) {
				if (dobAppUpdateVersion==0) {
					maxVersionApp.setIntState(Const.APP_MUST_UPDATE);
				}
			}
		}
		return maxVersionApp;
	}
	
	public AppUpdateTab getNeed2UpdateApp(int appId,int osType,double dobAppUpdateVersion) throws Exception {
		List<AppUpdateTab> appUpdateList = new ArrayList<AppUpdateTab>();
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("osType", osType);
		argsMap.put("dobAppUpdateVersion", dobAppUpdateVersion);
		argsMap.put("state", Const.APP_NOT_UPDATE);
		appUpdateList = this.hqlGetList("GET_APP_UPDATE_MAX", argsMap);
		AppUpdateTab appUpdateTab = getFirstFromList(appUpdateList);
		return appUpdateTab;
	}
	
	public AppUpdateTab getMaxVersionApp(int appId,int osType) throws Exception {
		List<AppUpdateTab> appUpdateList = new ArrayList<AppUpdateTab>();
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("osType", osType);
		appUpdateList = this.hqlGetList("GET_APP_UPDATE_MAX_VERSION", argsMap);
		AppUpdateTab appUpdateTab = getFirstFromList(appUpdateList);
		return appUpdateTab;
	}
	
	/**
	 * 检查的当前版本是否需要更新
	 * @param currentVersion
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-12-20下午04:23:28
	 */
	private boolean checkIsUpdate(double currentVersion,int appId,int osType,int state)throws Exception{
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("dobAppUpdateVersion", currentVersion);
		argsMap.put("osType",osType);
		argsMap.put("state", state);
		
		Long num = hqlGetNums("GET_APP_UPDATE_COUNT", argsMap);
		if (num.intValue()>0) {
			return true;
		}
		return false;
	}
	
	private AppUpdateTab getFirstFromList(List<AppUpdateTab> appUpdateList){
		if (appUpdateList!=null && appUpdateList.size()>0) {
			return appUpdateList.get(0);
		}else {
			return null;
		}
	}
}